<template>
  <div class="status-display">
    <div class="section-header">
      <div class="section-icon">📊</div>
      <h2>状态信息</h2>
    </div>
    
    <div class="question-container">
      <template v-if="currentQuestion">
        <div class="info-card">
          <div class="card-header">
            <span class="card-icon">❓</span>
            <span class="card-title">当前题目</span>
          </div>
          <div class="info-row">
            <span class="label">题目类型:</span>
            <span class="value">{{ currentQuestion.type }}</span>
          </div>
          <div class="info-row">
            <span class="label">题目内容:</span>
            <span class="value truncate">{{ currentQuestion.text }}</span>
          </div>
          <div class="info-row">
            <span class="label">答案:</span>
            <span class="value answer">{{ currentQuestion.answer }}</span>
          </div>
        </div>
      </template>
      <div v-else class="no-data">
        <div class="no-data-icon">📝</div>
        <div class="no-data-text">暂无活动题目</div>
        <div class="no-data-hint">开始解题后将显示题目信息</div>
      </div>
    </div>
    
    <div class="log-section">
      <div class="log-header">
        <span class="log-icon">📋</span>
        <span class="log-title">运行日志</span>
        <span class="log-count">{{ logs.length }}</span>
      </div>
      <div class="log-list">
        <div v-if="logs.length === 0" class="empty-logs">
          暂无日志记录
        </div>
        <div v-else v-for="(log, index) in logs" :key="index" class="log-entry">
          <span class="log-time">{{ formatTime(index) }}</span>
          <span class="log-content">{{ log }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { useQuizStore } from '../store/quizStore'

const quizStore = useQuizStore()
const currentQuestion = ref(null)
const logs = ref([])

// 处理消息的统一函数
function handleMessage(message) {
  const { type, data } = message
  
  if (type === 'questionUpdate' && data) {
    currentQuestion.value = data
  } else if (type === 'log' && data) {
    addLog(data)
  }
}

// 添加日志
function addLog(message) {
  logs.value.push(message)
  // 保持日志数量限制
  if (logs.value.length > 10) {
    logs.value.shift()
  }
}

// 格式化时间
function formatTime(index) {
  const now = new Date()
  // 为每个日志项生成一个时间戳
  const time = new Date(now.getTime() - (logs.value.length - index - 1) * 1000)
  return time.toLocaleTimeString('zh-CN', { 
    hour: '2-digit', 
    minute: '2-digit', 
    second: '2-digit' 
  })
}

// 生命周期钩子
onMounted(() => {
  chrome.runtime.onMessage.addListener(handleMessage)
})

onUnmounted(() => {
  chrome.runtime.onMessage.removeListener(handleMessage)
})
</script>

<style>
.status-display {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

/* 标题部分 */
.section-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 2px solid rgba(102, 126, 234, 0.1);
}

.section-icon {
  font-size: 20px;
}

.section-header h2 {
  font-size: 16px;
  font-weight: 600;
  margin: 0;
  color: #2c3e50;
}

/* 问题容器 */
.question-container {
  margin-bottom: 16px;
}

.info-card {
  background: linear-gradient(135deg, #f8f9fa, #e9ecef);
  border-radius: 8px;
  padding: 12px;
  border: 1px solid rgba(225, 232, 237, 0.5);
}

.card-header {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
}

.card-icon {
  font-size: 16px;
}

.card-title {
  font-size: 14px;
  font-weight: 600;
  color: #2c3e50;
}

.info-row {
  display: flex;
  margin-bottom: 8px;
  align-items: flex-start;
  gap: 8px;
}

.info-row:last-child {
  margin-bottom: 0;
}

.label {
  font-weight: 600;
  color: #495057;
  min-width: 80px;
  flex-shrink: 0;
  font-size: 13px;
}

.value {
  flex-grow: 1;
  color: #2c3e50;
  font-size: 13px;
  line-height: 1.4;
}

.value.answer {
  color: #28a745;
  font-weight: 600;
  background: rgba(40, 167, 69, 0.1);
  padding: 4px 8px;
  border-radius: 4px;
  border-left: 3px solid #28a745;
}

.truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 无数据状态 */
.no-data {
  text-align: center;
  padding: 24px 16px;
  color: #6c757d;
}

.no-data-icon {
  font-size: 32px;
  margin-bottom: 8px;
  opacity: 0.6;
}

.no-data-text {
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 4px;
}

.no-data-hint {
  font-size: 12px;
  opacity: 0.7;
}

/* 日志部分 */
.log-section {
  border-top: 1px solid rgba(102, 126, 234, 0.1);
  padding-top: 16px;
}

.log-header {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 12px;
  font-size: 14px;
  font-weight: 600;
  color: #2c3e50;
}

.log-icon {
  font-size: 16px;
}

.log-title {
  flex: 1;
}

.log-count {
  background: #667eea;
  color: white;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
}

/* 日志列表 */
.log-list {
  max-height: 120px;
  background: rgba(248, 249, 250, 0.8);
  border-radius: 6px;
  padding: 8px;
  border: 1px solid rgba(225, 232, 237, 0.5);
}

.empty-logs {
  text-align: center;
  color: #6c757d;
  font-size: 12px;
  padding: 16px;
  font-style: italic;
}

.log-entry {
  display: flex;
  gap: 8px;
  margin-bottom: 6px;
  font-size: 12px;
  line-height: 1.3;
  padding: 4px 0;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.log-entry:last-child {
  margin-bottom: 0;
  border-bottom: none;
}

.log-time {
  color: #6c757d;
  font-weight: 500;
  flex-shrink: 0;
  min-width: 60px;
}

.log-content {
  color: #495057;
  flex: 1;
  word-break: break-word;
}
</style> 